// pages/history/history.js
var that = this;

Page({

  /**
   * 页面的初始数据
   */
  data: {

    curYear: new Date().getFullYear(), // 年份
    curMonth: new Date().getMonth() + 1,// 月份 1-12
    day: new Date().getDate(), // 日期 1-31 若日期超过该月天数，月份自动增加
    header_show: true, // 主标题是否显示
    prev: true, // 上一个月按钮是否显示
    next: true, // 下一个月按钮是否显示
    habit_list:[]
  },

  // selectDate: function(e){
  //   console.log(e)
  

  //   txtlist: [], 
  //   //mystatus: [],
  //   results:[],
  //   this_month_record: [],
  //   my_habits:[],
  // },

  selectDate: function(date) {
    console.log(date);
    console.log(date.detail.date);

    this.setData({
      my_habits: []
    })

    var i=0;
    this.data.this_month_record.forEach((record) => {
      if(record.dates.includes(date.detail.date.split('-')[2])){
        console.log(record.name)
        var temp_habit = 'my_habits[' + i + ']';

        this.setData({
          [temp_habit]: record.name
        })
        
        i++;
      }
    });
  
  },
  
  prevMonth: function(res){
    this.setData({
      txtlist: []
    });

    var thisYear = res.detail.currentYear;
    var thisMonth = res.detail.currentMonth;
    //console.log(this.data.results)

    var records = this.data.results;

    this.show_hit_days(thisYear, thisMonth, records);
  },


  nextMonth: function(res){

    this.setData({
      txtlist: []
    });

    console.log("yearrr"+res.detail.currentYear);
    console.log("monnnnn"+res.detail.currentMonth);

    var thisYear = res.detail.currentYear;
    var thisMonth = res.detail.currentMonth;
    //console.log(this.data.results)

    var records = this.data.results;

    this.show_hit_days(thisYear, thisMonth, records);
    /*var this_month_record = [], this_month_days = [];

    records.forEach((record) => {
      var one_record = new Object();
      var temp_days = [];

      record.dates.forEach((date) => {
       
        console.log(date.split('-')[0])
        console.log(date.split('-')[1])
        console.log(date.split('-')[2])
        if (date.split('-')[0] == thisYear && date.split('-')[1] == thisMonth){
          temp_days.push( date.split('-')[2]);
          this_month_days.push(date.split('-')[2]);
        }
      });

      one_record.name = record.name;
      one_record.dates = temp_days;

      this_month_record.push(one_record);
    });

    console.log(this_month_record)
    this_month_days.forEach((i) =>{
      var temp = 'txtlist[' + (i-1) + ']';
      this.setData({
        [temp]: 'Hit',//4个汉字
      });
    });*/

  },
  
  show_hit_days: function(thisYear, thisMonth, records) {
    var this_month_record = [], this_month_days = [];

    records.forEach((record) => {
      var one_record = new Object();
      var temp_days = [];

      record.dates.forEach((date) => {

        if (date.split('-')[0] == thisYear && date.split('-')[1] == thisMonth) {
          temp_days.push(date.split('-')[2]);
          this_month_days.push(date.split('-')[2]);
        }
      });

      one_record.name = record.name;
      one_record.dates = temp_days;

      this_month_record.push(one_record);
    });

    console.log(this_month_record)
    this.setData({
      this_month_record: this_month_record
    });

    this_month_days.forEach((i) => {
      var temp = 'txtlist[' + (i - 1) + ']';
      this.setData({
        [temp]: 'Hit',//4个汉字
      });
    });

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    // var that=this
    // that.setData({
    //   habit_list: getApp().globalData.habit
    // })
    var that = this;

    //把history存入data
    wx.login({
      success: (res_code) => {
        if (res_code.code) {

          wx.request({
            url: getApp().globalData.my_server + "/minihabit/one_week_record",
            data: {
              code: res_code.code,
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
              //console.log(res.data.result)

              that.setData({
                results: res.data.result,
              })

              //console.log(that.data.results)

              //var i = 9;

             // var temp='txtlist['+i +']';
              //that.setData({
              //  [temp]: '文字文字文字文字',//4个汉字
                //['', '', '', '文字1\r\n实验', '', '', '文字2', '', '', '',]
              //})
              //that.setData({
              //  mystatus: [null,1,1]
              //})
              //console.log(that.data.time)
              //console.log(that.data.habit_list);
             

               // i++;

           
            },
            fail: function (res) {
              wx.showModal({
                title: '哎呀～',
                content: '读取失败',
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
              console.log("fail!", res)
            },
            complete: function (res) {

            }

          });
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });

    //读取当前年、月 ==========================??
    var date = new Date();
    var thisYear = date.getFullYear();
    var thisMonth = date.getMonth() + 1;
    console.log(thisYear + "oooooooo")
    console.log(thisMonth + "oooooooo")

    this.show_hit_days(thisYear, thisMonth, this.data.results);
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})